package com.biz.primus.ms.thirdlogin.oauth.service.weibo;

import com.biz.primus.model.thirdlogin.enums.OAuthTypes;
import com.biz.primus.ms.thirdlogin.dao.po.OAuthMember;
import com.biz.primus.ms.thirdlogin.oauth.service.OAuthServiceDeractor;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;

/**
 * @author jun.liu
 * @date 2017/9/29
 */
public class SinaWeiboOAuthService extends OAuthServiceDeractor
{

    private static final String PROTECTED_RESOURCE_URL = "https://api.weibo.com/oauth2/get_token_info";

    public SinaWeiboOAuthService(OAuthService oAuthService) {
        super(oAuthService, OAuthTypes.SINA_WEIBO);
    }

    @Override
    public OAuthMember getOAuthMember(Token accessToken)
    {
        OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL);
        this.signRequest(accessToken, request);
        Response response = request.send();
        OAuthMember oAuthMember = new OAuthMember();
        oAuthMember.setOAuthType(getOAuthType());
        oAuthMember.setOAuthId(JSONPath.eval(JSON.parse(response.getBody()), "$.uid").toString());
        return oAuthMember;
    }

}
